home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap18 / Metafile / Metafile.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.5 KB  |  111 lines

  1. /*----------------------------------------------
  2.    METAFILE.C -- Metafile Demonstration Program
  3.                  (c) Charles Petzold, 1998
  4.   ----------------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName [] = TEXT ("Metafile") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.  
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Metafile Demonstration"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  54. {
  55.      static HMETAFILE hmf ;
  56.      static int       cxClient, cyClient ;
  57.      HBRUSH           hBrush ;
  58.      HDC              hdc, hdcMeta ;
  59.      int              x, y ;
  60.      PAINTSTRUCT      ps ;
  61.      
  62.      switch (message)
  63.      {
  64.      case WM_CREATE:
  65.           hdcMeta = CreateMetaFile (NULL) ;
  66.           hBrush  = CreateSolidBrush (RGB (0, 0, 255)) ;
  67.           
  68.           Rectangle (hdcMeta, 0, 0, 100, 100) ;
  69.           
  70.           MoveToEx (hdcMeta,   0,   0, NULL) ;
  71.           LineTo   (hdcMeta, 100, 100) ;
  72.           MoveToEx (hdcMeta,   0, 100, NULL) ;
  73.           LineTo   (hdcMeta, 100,   0) ;
  74.           
  75.           SelectObject (hdcMeta, hBrush) ;
  76.           Ellipse (hdcMeta, 20, 20, 80, 80) ;
  77.           
  78.           hmf = CloseMetaFile (hdcMeta) ;
  79.           
  80.           DeleteObject (hBrush) ;
  81.           return 0 ;
  82.           
  83.      case WM_SIZE:
  84.           cxClient = LOWORD (lParam) ;
  85.           cyClient = HIWORD (lParam) ;
  86.           return 0 ;
  87.           
  88.      case WM_PAINT:
  89.           hdc = BeginPaint (hwnd, &ps) ;
  90.           
  91.           SetMapMode (hdc, MM_ANISOTROPIC) ;
  92.           SetWindowExtEx (hdc, 1000, 1000, NULL) ;
  93.           SetViewportExtEx (hdc, cxClient, cyClient, NULL) ;
  94.           
  95.           for (x = 0 ; x < 10 ; x++)
  96.           for (y = 0 ; y < 10 ; y++)
  97.           {
  98.                SetWindowOrgEx (hdc, -100 * x, -100 * y, NULL) ;
  99.                PlayMetaFile (hdc, hmf) ;
  100.           }
  101.           EndPaint (hwnd, &ps) ;
  102.           return 0 ;
  103.                
  104.      case WM_DESTROY:
  105.           DeleteMetaFile (hmf) ;
  106.           PostQuitMessage (0) ;
  107.           return 0 ;
  108.      }
  109.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  110. }
  111.